package com.woniuxy.creditsystem.loanmag.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.common.bean.*;
import com.woniuxy.creditsystem.common.bean.ResultMsg;
import com.woniuxy.creditsystem.loanmag.service.LoanService;
import com.woniuxy.creditsystem.util.MonthUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 放款管理模块的控制层 去
 * by 汪汪
 */
@RestController
@RequestMapping("/loan")
public class LoanController {
    @Resource
    private LoanService loanService;
    @RequestMapping(value = "getLoans")
    public ResultMsg<List<LoansInfo>> getTableData(@RequestBody LoanQuery loanQuery){
        System.out.println(loanQuery);
        ResultMsg<List<LoansInfo>> msg = new ResultMsg<>();
        PageInfo<LoansInfo> pageInfo = loanService.findByQuery(loanQuery);
        msg.setData(pageInfo.getList());
        msg.setMsg(pageInfo.getTotal()+"");
        msg.setCode(200);
        return msg;
    }


    @RequestMapping(value="agree") //订单编号、还款期数、还款类型、
    //同意放款方法
    public ResultMsg agree(String orderid){
        //得到全部信息
        Detail detail = loanService.findByorderid(orderid);
        System.out.println(detail);
        //添加还款计划
        for (int i = 0; i <detail.getPeriods(); i++) {
            ReturnPlanInfo planInfo = new ReturnPlanInfo();
            planInfo.setDataNum(i+1);//设置期数
            //设置还款日期
            Date date = MonthUtil.subMonth(new Date(),i+1);
            planInfo.setBillTime(date);
            //计算利息
            BigDecimal money = detail.getMoney();//计算利息
            BigDecimal interest = money.multiply(new BigDecimal(detail.getMonthInterestRate() + "")).setScale(2, BigDecimal.ROUND_HALF_UP);
            planInfo.setInterest(interest);//设置利息
            //一开始罚息都是零
            planInfo.setPenaltyInteres(new BigDecimal(0));
            //设置还款状态
            planInfo.setAlsoStatus("未还款");
            //设置客户实际还款时间
            planInfo.setAlsoTime(null);
            //设置操作
            planInfo.setOperation("未结清");
            //设置客户ID
            planInfo.setFkCustomerId(detail.getCid());
            //设置当期还款
            BigDecimal currentmoney=null;
            //判断还款类型
            if (detail.getRefundType().equals("先息后本")){
                //每个月只还利息，不还本金
                currentmoney = new BigDecimal("0");
                planInfo.setPrincipal(currentmoney);//设置当期还款
                if (i==detail.getPeriods()-1){
                    //当这是最后一期，全部本金一次还
                    currentmoney=detail.getMoney();
                    planInfo.setPrincipal(currentmoney);
                }

            }else{
                //当期还款,保留两位小数
                currentmoney = detail.getMoney().divide(new BigDecimal(detail.getPeriods() + ""), 2, BigDecimal.ROUND_HALF_UP);
                planInfo.setPrincipal(currentmoney);//设置当期还款
            }
            //计算当前待还金额;当期待还金额=当前本金+当期利息-已还款金额
            BigDecimal waittrturn = currentmoney.add(interest);
            //设置待还金额
            planInfo.setWaitMoney(waittrturn);
            //设置所属订单bianhao
            planInfo.setOrderNum(orderid);
            //将账单加入数据库
            loanService.saveReturn(planInfo);
        }
        //将用户的状态设置为已放款；
        loanService.setStatus(detail.getOrderid(),"已放款");
        //同意放款，则生成还款项，并将待放款设置为已放款
        ResultMsg msg = new ResultMsg();
        msg.setMsg("已放款");
        return msg;
    }

    //得到全部user的信息
    @RequestMapping(value="getusers")
    public ResultMsg<List<UserInfo>> getusers(){
        //查询所有的用户信息
        List<UserInfo> list = loanService.findusers();
        ResultMsg msg = new ResultMsg();
        msg.setCode(200);
        msg.setData(list);
        return msg;
    }

    //设置已催收的方法
    @RequestMapping("callloans")
    public ResultMsg callloans(@RequestBody List<LoansInfo> list){
        System.out.println(list);
        //向数据库中添加催收数据
        for (int i = 0; i < list.size(); i++) {
            //创建参数
            CollectionInfo collectionInfo = new CollectionInfo();
            collectionInfo.setOrderNum(list.get(i).getOrderid());//设置订单编号
            collectionInfo.setCustomerName(list.get(i).getClientName());//设置客户姓名
            collectionInfo.setColStatus("未还款");  //设置催收状态
            collectionInfo.setColDesc("正在催收");  //催收进度
            collectionInfo.setAppointTime(new Date());//设置委派催收时间
            collectionInfo.setFkUserId(list.get(i).getUserid());//设置催收人外键
            System.out.println(collectionInfo);
            //存入数据库
            loanService.savaappoint(collectionInfo);
            //同时将订单表的状态设置为已催收
            loanService.setStatus(collectionInfo.getOrderNum(),"已催收");
        }
        ResultMsg msg = new ResultMsg();
        msg.setCode(200);
        msg.setMsg("已委派催收");
        return msg;
    }

    //结清当前期数的方法
    @RequestMapping("clean")
    public ResultMsg clean(@RequestBody List<ReturnPlanInfo> list){
        System.out.println("asdasd"+list);
        for (int i = 0; i < list.size(); i++) {
            //修改还款计划表订单状态
            ReturnPlanInfo planInfo = new ReturnPlanInfo();
            //已还款期数
            planInfo.setDataNum(list.get(i).getDataNum());
            //设置当前还款账单
            planInfo.setAlsoStatus("已还款");
            //设置还款时间
            planInfo.setAlsoTime(new Date());
            //设置待还金额
            planInfo.setWaitMoney(new BigDecimal(0));
            //设置用户id
            planInfo.setFkCustomerId(list.get(i).getFkCustomerId());
            //设置逾期天数
            planInfo.setOverdueDays(0);
            System.out.println(planInfo);
            //改变数据库
            loanService.changeRe(planInfo);
            //判断是否已结清
            Boolean isclean = loanService.isClean(list.get(i).getFkCustomerId());

            int day=0;
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setNum(list.get(i).getOrderNum()); //设置订单编号
            if (isclean){
                //并且将逾期天数设为0;
                orderInfo.setOverduedays(day);
                orderInfo.setStatus("已结清"); //设置订单状态
            }else{
                //如果没有还清，查找最大逾期天数
                day = loanService.getMaxdaysbyOid(list.get(i).getOrderNum());
                //订单表赋值逾期天数
                //状态、逾期天数、订单
                orderInfo.setStatus("逾期中");
                orderInfo.setOverduedays(day);
            }
            loanService.setOrder(orderInfo);
        }
        ResultMsg msg = new ResultMsg();
        msg.setCode(200);
        msg.setMsg("已结清");
        return msg;
    }

    //通过订单编号查询详情信息
    @RequestMapping("getallByOid")
    public ResultMsg getallByOid(String orderid){
        Detail detail = loanService.findByorderid(orderid);
        System.out.println(detail);
        ResultMsg msg = new ResultMsg();
        msg.setCode(200);
        msg.setData(detail);
        return msg;
    }
}
